﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using LM.Negocio.Objetos;

namespace LM.Repositorios.Maps
{
    public class ClienteMap : EntityTypeConfiguration<Cliente>
    {
        public ClienteMap()
        {
            this.HasKey(c => c.Id);
            this.Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("Id");
            
            this.Property(c => c.Codigo).HasColumnName("Codigo").IsRequired();
            this.Property(c => c.Nome).HasColumnName("Nome").IsRequired().HasMaxLength(150);
            this.Property(c => c.Cpf).HasColumnName("Cpf").IsRequired();
            this.Property(c => c.Cnh).HasColumnName("Cnh").IsRequired();
            this.Property(c => c.TipoDoCliente).HasColumnName("TipoDoCliente").HasColumnType("int").IsRequired();
            
            HasMany(x => x.Endereco).WithMany().Map(map =>
            {
                map.ToTable("CLIENTE_ENDERECO");
                map.MapLeftKey("IdCliente");
                map.MapRightKey("IdEndereco");
            });

            HasMany(x => x.Telefones).WithMany().Map(map =>
            {
                map.ToTable("CLIENTE_TELEFONE");
                map.MapLeftKey("IdCliente");
                map.MapRightKey("IdTelefone");
            });

            this.ToTable("CLIENTE");
        }
    }
}
